#!/bin/bash

# split the library into individual source files for every single function,
# compile them seperately and pack them all together into one big library.


LIBSOURCES="stm8s_adc1.c stm8s_awu.c stm8s_beep.c stm8s_clk.c stm8s_exti.c stm8s_flash.c stm8s_gpio.c
stm8s_i2c.c
stm8s_itc.c
stm8s_iwdg.c
stm8s_rst.c
stm8s_spi.c
stm8s_tim1.c
stm8s_tim2.c
stm8s_tim4.c
stm8s_uart1.c
stm8s_wwdg.c"

# not used for stm8s103:
#stm8s_adc2.c
#stm8s_can.c \
#stm8s_tim3.c \
#stm8s_tim5.c \
#stm8s_tim6.c \
#stm8s_uart2.c \
#stm8s_uart3.c \
#stm8s_uart4.c


splitit() {
#awk '/\/\*\*\r/{n++; file="x/$1-" n ".c";print "#include \"inc.h\"">file}{print >file }' $1
awk "/\/\*\*\r/{n++; file=\"$TMPDIR/$1-\" n \".c\";print \"#include \\\"$1.h\\\"\">file}{print >file }" $1
cd $TMPDIR
sed "1 d" $1-1.c > $1.h
rm $1-1.c
cd -
}

TMPDIR=$(mktemp -dp.)
echo "using $TMPDIR"
for i in $LIBSOURCES; do splitit $i; done
cp Makefile $TMPDIR
make -C $TMPDIR
mv $TMPDIR/stm8s103.lib .
